﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Valor.DBUtility.MSSQLHelper;
using xTeam.HengLe.Entity;

namespace xTeam.HengLe.DataAccess {
    public class GuestBookDataAccess {
        private bool _isCN = true;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="connectionString"></param>
        public GuestBookDataAccess(string connectionString, string langName) {
            SQLProcedureHelper.ConnectionString = connectionString;
            this._isCN = langName.ToLower() == "en-us" ? false : true;
        }
        /// <summary>
        /// 留言簿的添加修改
        /// </summary>
        /// <param name="guestBook">留言簿实体</param>
        /// <returns>留言簿编号</returns>
        public int Edit(EGuestBook guestBook) {
            int result = 0;
            SqlParameter[] parameters =
            {
                new SqlParameter("@ProductId",SqlDbType.Int){Value = guestBook.ProductId },
                new SqlParameter("@GuestBookId",SqlDbType.Int){Value = guestBook.GuestBookId },
                new SqlParameter("@NickName",SqlDbType.NVarChar,20){Value = guestBook.NickName },
                new SqlParameter("@Email",SqlDbType.NVarChar,50){Value = guestBook.Email },
                new SqlParameter("@Content",SqlDbType.NVarChar,500){Value = guestBook.Content },
                new SqlParameter("@ReContent",SqlDbType.NVarChar,500){Value = guestBook.ReContent }
            };
            string procName = this._isCN
                ? (guestBook.GuestBookId > 0 ? "GuestBook_Update" : "GuestBook_Append")
                : (guestBook.GuestBookId > 0 ? "GuestBook_Update_en" : "GuestBook_Append_en");
            result = Convert.ToInt32(SQLProcedureHelper.ExecuteScalar(procName, parameters));
            return result;
        }
        /// <summary>
        /// 删除留言簿
        /// </summary>
        /// <param name="guestBookId">留言簿编号</param>
        /// <returns>是否成功</returns>
        public int Delete(int guestBookId) {

            return SQLProcedureHelper.ExecuteNonQuery(this._isCN ? "guestBook_delete" : "guestBook_delete_en"
                , new SqlParameter[] { 
                    new SqlParameter("@GuestBookId",SqlDbType.Int) {Value= guestBookId } 
                });

        }

        /// <summary>
        /// 条件查询留言簿
        /// </summary>
        /// <returns>留言簿列表</returns>
        public List<EGuestBook> Search(int productId) {
            List<EGuestBook> guestBookList = new List<EGuestBook>();
            SqlParameter[] parameters = 
            {
                //new SqlParameter("@Key",SqlDbType.NVarChar,50){Value = key },
                //new SqlParameter("@Classifyid",SqlDbType.Int){Value = classifyid},
                // new SqlParameter("@isvalid",SqlDbType.Bit){Value = isvalid}
                new SqlParameter("@ProductId",SqlDbType.Int){Value = productId },
                new SqlParameter("@PageIndex",SqlDbType.Int){Value=1},
                new SqlParameter("@PageSize",SqlDbType.Int){Value=(int.MaxValue-1)}
           };

            SqlDataReader reader = SQLProcedureHelper.ExecuteReader(this._isCN ? "GuestBook_SelectList" : "GuestBook_SelectList_en", parameters);

            while (reader.Read()) {
                guestBookList.Add(new EGuestBook() {
                    ProductId = Convert.ToInt32(reader["ProductId"]),
                    GuestBookId = Convert.ToInt32(reader["GuestBookId"]),
                    NickName = Convert.ToString(reader["NickName"]),
                    Email = Convert.ToString(reader["Email"]),
                    Content = Convert.ToString(reader["Content"]),
                    ReContent = Convert.ToString(reader["ReContent"]),
                    AddDate = Convert.ToDateTime(reader["AddDate"])
                });
            }

            reader.Close();
            return guestBookList;
        }

        public EGuestBook GetGuestBookById(int guestBookId) {
            EGuestBook guestBook = null;
            SqlParameter[] parameters = 
            {
                new SqlParameter("@GuestBookId",SqlDbType.Int){Value = guestBookId}
            };
            SqlDataReader reader = SQLProcedureHelper.ExecuteReader(this._isCN ? "GuestBook_Select" : "GuestBook_Select_en", parameters);
            if (reader.HasRows) {
                guestBook = new EGuestBook();
                reader.Read();

                guestBook.ProductId = Convert.ToInt32(reader["ProductId"]);
                guestBook.GuestBookId = Convert.ToInt32(reader["GuestBookId"]);
                guestBook.NickName = Convert.ToString(reader["NickName"]);
                guestBook.Email = Convert.ToString(reader["Email"]);
                guestBook.Content = Convert.ToString(reader["Content"]);
                guestBook.ReContent = Convert.ToString(reader["ReContent"]);
                guestBook.AddDate = Convert.ToDateTime(reader["AddDate"]);
            }
            reader.Close();
            return guestBook;

        }
    }
}
